package com.leaf.lowcode.service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.leaf.lowcode.API.DoorFunctionApi;
import com.leaf.lowcode.entity.DoorPersonCountDTO;
import com.leaf.lowcode.entity.Dooreven;
import com.leaf.lowcode.entity.door_control.EventsRequest;
import com.leaf.lowcode.mapper.DoorevenPersonMapper;
import com.leaf.lowcode.utils.RegularInstance;
import com.leaf.lowcode.utils.jsontobean.JsonToBeanV;
import com.tencent.welink.iot.SubDeviceDTO;
import com.tencent.welink.iot.WelinkHttp;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;


/**
 *
 * @DoorPersonCount 大楼内人员统计
 *
 * @PersonCount=PersonIn-PersonOut   大楼内人员统计
 *
 */

@Component
public class DoorPersonCount {

    @Autowired
    private DoorevenPersonMapper doorevenPersonMapper;

  @Scheduled(initialDelay = 10*1000, fixedRate = 10*1000)
    public void execute() {
        List<SubDeviceDTO> subDeviceDTOList = RegularInstance.subDeviceDTOList;
          /*  String pattern = "YYYY-MM-dd'T'HH:mm:ssZZ";
        String data=DateFormatUtils.format(new Date(),pattern);*/

        Date cur = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.add(Calendar.DAY_OF_MONTH,0);
        //一天的开始时间 yyyy:MM:dd 00:00:00
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        Date dayStart = calendar.getTime();
        String pattern = "YYYY-MM-dd'T'HH:mm:ssZZ";
        String startStr =DateFormatUtils.format(dayStart,pattern);
        System.out.println("startStr"+startStr);
        //一天的结束时间 yyyy:MM:dd 00:00:00
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
        calendar.set(Calendar.MILLISECOND,999);
        Date dayEnd = calendar.getTime();
        String endStr=DateFormatUtils.format(dayEnd,pattern);
        System.out.println("startStr"+endStr);


        DoorFunctionApi doorFunctionApi=  new DoorFunctionApi();
        EventsRequest eventsRequest=new EventsRequest();
        eventsRequest.setPageNo(1);
        eventsRequest.setPageSize(1000);
        eventsRequest.setStartTime(startStr);
        eventsRequest.setEndTime(endStr);
        String doorcount=doorFunctionApi.events(eventsRequest);
        JSONObject jsonObject = JSONObject.parseObject(doorcount);
        JSONArray list = (JSONArray) ((JSONObject) jsonObject.get("data")).get("list");
        System.out.println(list);
        Dooreven dooreven =new Dooreven();
        DoorPersonCountDTO doorevenDTO= new DoorPersonCountDTO();
        for(int i=0;i<list.size();i++) {
            JSONObject object = (JSONObject) list.get(i);
            JsonToBeanV<Dooreven> jsonToBeanV = new JsonToBeanV<Dooreven>();
            dooreven = jsonToBeanV.tansfrom(dooreven, object);
            BeanUtils.copyProperties(dooreven, doorevenDTO);
            if (doorevenDTO.getDoorName().contains("1F")==true) {
              doorevenDTO.setOrgName(doorevenDTO.getOrgName().replaceFirst("默认组织/",""));
                DoorPersonCountDTO flog = doorevenPersonMapper.selectId(doorevenDTO.getEventId());

                if (flog == null) {
                   System.out.println("不存在");
                    doorevenPersonMapper.insert(doorevenDTO);
                                System.out.println("门禁事件");
                                //  System.out.println("======================门禁卡========================"+readerdoorstatesend.getIndexCode());
                                JSONObject standardData = new JSONObject();
                                JSONObject properties  = new JSONObject();
                                JSONObject propertie  = new JSONObject();
                                standardData.put("reportTs", System.currentTimeMillis());
                                JSONObject profile = new JSONObject();
                                profile.put("modelId", 200735);
                                profile.put("poiCode", "w0703003");
                                standardData.put("profile", profile);
                                WelinkHttp wh = new WelinkHttp(RegularInstance.registerData1, RegularInstance.loginData);
                                JSONObject events = new JSONObject();
                                JSONObject event = new JSONObject();
                                int PersonIn=doorevenPersonMapper.count("1",startStr,endStr);
                                int PersonOut=doorevenPersonMapper.count("0",startStr,endStr);
                                int PersonCont=PersonIn-PersonOut;
                                System.out.println("人员统计===>"+PersonCont);
                                propertie.put("timeNow", new Date());
                                propertie.put("doorPerson", PersonCont);
                                propertie.put("doorOut", PersonOut);
                                propertie.put("doorIn",PersonIn);
                                properties.put("doorInAndOut",propertie);
                                standardData.put("properties", properties);
                                standardData.put("events", events);
                                //standardData.put("properties", "{}");
                                try {
                                      System.out.println("上传数据");
                                    String devRunEvent = JSONObject.toJSONString(standardData, SerializerFeature.WriteMapNullValue);
                                      System.out.println("发送设备运行时事件数据==" + devRunEvent);
                                        System.out.println("--------------------------------开始传输设备运行时事件数据");
                                    wh.sendMessage(RegularInstance.loginData.load(), 80000, Long.parseLong("300000000000207659"), devRunEvent, true);
                                } catch (Exception e) {
                                    System.out.println("设备运行时事件数据传输出现异常");
                                }
                } else {
                    System.out.println("存在");
//                    int PersonIn=doorevenPersonMapper.count("1",startStr,endStr);
//                    System.out.println(doorevenPersonMapper.count("1",startStr,endStr));
//                    System.out.println("进入"+PersonIn);
//                    int PersonOut=doorevenPersonMapper.count("0",startStr,endStr);
//                    System.out.println("出门"+PersonOut);
//
//                    int PersonCont=PersonIn-PersonOut;
//                    System.out.println("人员统计===>"+PersonCont);
//
//                    JSONObject standardData = new JSONObject();
//                    JSONObject properties  = new JSONObject();
//                    JSONObject propertie  = new JSONObject();
//                    standardData.put("reportTs", System.currentTimeMillis());
//                    JSONObject profile = new JSONObject();
//                    profile.put("modelId", 200735);
//                    profile.put("poiCode", "w0703003");
//                    standardData.put("profile", profile);
//                    WelinkHttp wh = new WelinkHttp(RegularInstance.registerData1, RegularInstance.loginData);
//                    JSONObject events = new JSONObject();
//                    JSONObject event = new JSONObject();
//                    System.out.println("人员统计===>"+PersonCont);
//                    propertie.put("timeNow", new Date());
//                    propertie.put("doorPerson", PersonCont);
//                    propertie.put("doorOut", PersonOut);
//                    propertie.put("doorIn",PersonIn);
//                    properties.put("doorInAndOut",propertie);
//                    standardData.put("properties", properties);
//                    standardData.put("events", events);
//                    //standardData.put("properties", "{}");
//                    try {
//                        System.out.println("上传数据");
//                        String devRunEvent = JSONObject.toJSONString(standardData, SerializerFeature.WriteMapNullValue);
//                        System.out.println("发送设备运行时事件数据==" + devRunEvent);
//                        System.out.println("--------------------------------开始传输设备运行时事件数据");
//                        wh.sendMessage(RegularInstance.loginData.load(), 80000, Long.parseLong("300000000000207659"), devRunEvent, true);
//                    } catch (Exception e) {
//                        System.out.println("设备运行时事件数据传输出现异常");
//                    }
                }
            }
        }

      //  System.out.println(data);

    }
}
